<template>
	<!-- 通用版本 -->
	<view>
		<!-- 头部搜索框 -->
		<view class="topSearch">
			<zy-forms :model="model" :rules="rules" ref="ruleForm" labelWidth='200'>
				<zy-form-item :label="$t('homepage.WarehouseManage.deliveryNoteNum') + $t('public.colon')" required>
					<zy-input v-model="model.billNo" @confirm="confirmHandle" sufSlot>
						<i class="iconfont icon-saoyisao" @click="scanHandle" v-if="!model.billNo"></i>
						<i class="iconfont icon-qingchu" @click="model.billNo=''" v-else></i>
					</zy-input>
				</zy-form-item>
				<zy-form-item :label="$t('homepage.WarehouseManage.harvestUnit') + $t('public.colon')" :border="false" required>
					<zy-select :selectList="selectList" @lineHandler="lineHandler" :disabled="true" clearable>
					</zy-select>
				</zy-form-item>
			</zy-forms>
		</view>
		<!-- 搜索无记录 -->
		<zy-empty v-if="list.length==0"></zy-empty>
		<!-- 明细列表 -->
		<zy-group v-for="(item,index) in list" :key="index" model="insideCard" title="" v-else>
			<zy-forms :model="item" border labelWidth="120">
				<zy-form-item :label="$t('homepage.ProductionMonitor.materialCodeTitle')"><text class="formText">{{item.productCode || $t('public.null')}}</text>
				</zy-form-item>
				<zy-form-item :label="$t('homepage.ProductionMonitor.materialNameTitle')"><text class="formText">{{item.productName || $t('public.null')}}</text>
				</zy-form-item>
				<zy-form-item :label="$t('public.materialSpec') + $t('public.colon')"><text class="formText">{{item.productScale || $t('public.null')}}</text>
				</zy-form-item>
				<zy-form-item :label="$t('public.batchNo') + $t('public.colon')"><text class="formText">{{item.smallBarcode || $t('public.null')}}</text>
				</zy-form-item>
				<zy-form-item :label="$t('homepage.ProductionMonitor.numTitle')"><text class="formText">{{item.includeQty || $t('public.null')}}</text>
				</zy-form-item>
			</zy-forms>
		</zy-group>
		<!-- 底部按钮 -->
		<bottomBtn fixed type="danger" @btn="resetBtn" :disabled="list.length>0?false:true">
		</bottomBtn>
	</view>
</template>
<script>
	import {
		syncWms
	} from "@/api/homepage.js"
	import scanData from '@/common/scan.js';
	export default {
		components: {},
		data() {
			return {
				//表单value
				model: {
					billNo: '',
					alias: ''
				},
				alias: "", //收获单位下拉框key值
				//表单校验
				rules: [{
					required: true,
					name: "billNo",
					checkType: "notnull",
					checkRule: "",
					message: this.$t('public.pleaseInput')+this.$t('homepage.WarehouseManage.deliveryNoteNum')
				}, {
					required: true,
					name: "alias",
					checkType: "notnull",
					checkRule: "",
					message: this.$t('public.pleaseSelect')+this.$t('homepage.WarehouseManage.harvestUnit')
				}],
				// 收获单位下拉框数据
				selectList: [{
					label: this.$t('homepage.WarehouseManage.yjj'),
					value: "yjj"
				}, {
					label: this.$t('homepage.WarehouseManage.kbt'),
					value: "kbt"
				}],
				list: [], // 表单数据
			}
		},
		onLoad() {
			// 移除监听“添加扫描”弹窗事件
			uni.$off('scancodepopdate')
			//PDA扫描主页指示单号
			var _this = this
			uni.$on('scancodedate', function(data) {
				_this.model.billNo = data.code
				_this.searchList()
			})
		},
		onUnload() {
			// 移除监听事件      
			uni.$off('scancodedate')
		},
		methods: {
			// 二维码扫码事件
			scanHandle(field) {
				scanData.getScanData()
				uni.$once("barScan", (rel) => {
					this.model.billNo = rel
					this.searchList()
				})
			},
			// input框回车事件
			confirmHandle(e) {
				this.model.billNo = e
				this.searchList()
			},
			// 下拉框选择事件
			lineHandler(evt) {
				this.model.alias = evt.label
				this.alias = evt.value
				if (this.model.billNo) {
					this.searchList()
				} else {
					uni.showToast({
						title: this.$t('public.pleaseInput')+this.$t('homepage.WarehouseManage.deliveryNoteNum'),
						icon: "none",
						duration: 3000
					})
				}
			},
			// 查询事件
			searchList() {
				this.list = []
				if (this.model.billNo && this.alias) {
					syncWms({
						billNo: this.model.billNo,
						alias: this.alias,
					}).then((res) => {
						if (res.code == 0) {
							this.list = res.data
						} else {
							uni.showToast({
								title: res.message,
								icon: "none",
								duration: 3000
							})
						}
					}).catch((res) => {
						uni.showToast({
							title: res.message,
							icon: "none",
							duration: 3000
						})
					})
				}
			},
			// 重置事件
			resetBtn() {
				uni.redirectTo({
					url: "/pages/HomePage/WarehouseManage/StorageManagement/MaterialInstorage/index"
				})
			},
		}
	}
</script>
<style lang="scss" scoped>
	.topSearch {
		background: #ffffff;
		padding: 0 30rpx;
		width: calc(100% - 60rpx);
	}

	.bottomBtn {
		background: #ffffff;
	}
</style>